package team.sun.sss.controller.log;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import team.sun.sss.dao.log.BreakRuleDao;
import team.sun.sss.pojo.log.BreakRule;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;


@Controller
public class DownEx {
    @Autowired
    private BreakRuleDao breakRuleDao;

    @RequestMapping(value = "/DownEx", method = RequestMethod.GET)
    public void downloadsExcelDown(HttpServletResponse response) throws IOException {
        List<Map<String ,Object>> bgmExcelDownloads = breakRuleDao.getBreakRuleList();
        System.out.println(bgmExcelDownloads);

        HSSFWorkbook wb = new HSSFWorkbook();


        HSSFSheet sheet = wb.createSheet("违纪处分信息数据");

        HSSFRow row = null;


        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue("违纪处分列表");//为第一行单元格设值

        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(rowRegion);

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("学号");//为第一个单元格设值
        row.createCell(1).setCellValue("年级");//为第二个单元格设值
        row.createCell(2).setCellValue("二级学院");//为第三个单元格设值
        row.createCell(3).setCellValue("专业");//为第四个单元格设值
        row.createCell(4).setCellValue("姓名");
        row.createCell(5).setCellValue("处分期");
        row.createCell(6).setCellValue("违纪类型");
        row.createCell(7).setCellValue("处分等级");
        row.createCell(8).setCellValue("处分状态");
        //遍历所获取的数据
        for (int i = 0; i < bgmExcelDownloads.size(); i++) {
            row = sheet.createRow(i + 2);
            Map breakRule = bgmExcelDownloads.get(i);
            row.createCell(0).setCellValue(breakRule.get("stu_id").toString());
            row.createCell(1).setCellValue(breakRule.get("stu_grade").toString());
            row.createCell(2).setCellValue(breakRule.get("stu_college").toString());
            row.createCell(3).setCellValue(breakRule.get("stu_major").toString());
            row.createCell(4).setCellValue(breakRule.get("stu_name").toString());
            row.createCell(5).setCellValue(breakRule.get("bre_time").toString()+"-"+breakRule.get("bre_relieve_time").toString());
            row.createCell(6).setCellValue(breakRule.get("bre_type").toString());
            row.createCell(7).setCellValue(breakRule.get("bre_level").toString());
            row.createCell(8).setCellValue(breakRule.get("bre_state").toString());
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));
        //列宽自适应
        for (int i = 0; i <= 9; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=违纪处分.xls");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();

    }

}
